package com.woniuxy.user.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.QueryEntity;
import com.woniuxy.commons.entity.User;
import com.woniuxy.commons.utils.ResStatus;
import com.woniuxy.commons.utils.ResponseResult;
import com.woniuxy.user.service.MsmService;
import com.woniuxy.user.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @Resource
    MsmService msmService;

    @GetMapping("/findAll/{currentPage}/{pageSize}")
    public ResponseResult findAll(@PathVariable int currentPage, @PathVariable int pageSize) {
        return userService.findAll(currentPage, pageSize);
    }

    @GetMapping("/findAll")
    public ResponseResult findAll() {
        return userService.findAll();
    }

    @GetMapping("/findByRecord/{id}")
    public ResponseResult findByRecord(@PathVariable(value="id") int id) {
        return userService.findByRecord(id);
    }

    @PostMapping("/deleteOne/{id}")
    public ResponseResult deleteOne(@PathVariable(value = "id") int id) {
        return userService.deleteOne(id);
    }

    @PostMapping("/deleteSome")
    public ResponseResult deleteSome(@RequestBody int[] id) {
        return userService.deleteSome(id);
    }

    @GetMapping("/findByName/{name}")
    public ResponseResult findByName(@PathVariable String name) {
        String newName = null;
        try {
            newName = URLDecoder.decode(name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return userService.findByName(newName);
    }

    @GetMapping("/findStatus/{id}")
    public ResponseResult findStatus(@PathVariable int id){
           return userService.findStatus(id);
    }

    @PostMapping("/updateStatus")
    public ResponseResult updateStatus(@RequestBody User user){
           return userService.updateStatus(user);
    }

    @PostMapping("/findByCondition")
    public ResponseResult<PageInfo<User>> findByCondition(@RequestBody QueryEntity<User> queryEntity){
        return userService.findByCondition(queryEntity);
    }

    @GetMapping("/findByAddress/{id}")
    public ResponseResult findByAddress(@PathVariable int id){
          return userService.findByAddress(id);
    }

    @PostMapping("/send")
    public ResponseResult send(@RequestBody String[] phone) {

        boolean send = msmService.send(phone);
        if (send) {
            return new ResponseResult(200,"成功",null, ResStatus.SUCCESS);
        }
        return new ResponseResult(200,"成功",null, ResStatus.FAIL);
    }
}


